<?php
// 简化调试脚本

echo "检查环境...\n";

// 从.env文件读取数据库信息
$envFile = '.env';
if (file_exists($envFile)) {
    $envContent = file_get_contents($envFile);
    preg_match('/database\s*=\s*(\w+)/', $envContent, $dbMatches);
    preg_match('/username\s*=\s*(\w+)/', $envContent, $userMatches);
    preg_match('/password\s*=\s*(\w+)/', $envContent, $passMatches);
    
    $dbname = $dbMatches[1] ?? 'yingyang';
    $username = $userMatches[1] ?? 'root';
    $password = $passMatches[1] ?? 'root';
    
    echo "数据库配置:\n";
    echo "- 数据库: " . $dbname . "\n";
    echo "- 用户: " . $username . "\n";
    echo "- 密码: " . (empty($password) ? '空' : '****') . "\n\n";
    
    try {
        $pdo = new PDO("mysql:host=127.0.0.1;dbname={$dbname};charset=utf8mb4", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        // 检查是否有未处理的菜品
        $stmt = $pdo->prepare("SELECT id, name FROM fa_food_dish WHERE ai_called = 0 LIMIT 5");
        $stmt->execute();
        $dishes = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "未处理的菜品数量: " . count($dishes) . "\n";
        if (!empty($dishes)) {
            foreach ($dishes as $dish) {
                echo "- ID: " . $dish['id'] . ", 名称: " . $dish['name'] . "\n";
            }
        } else {
            echo "没有未处理的菜品\n";
        }
        
        // 检查API密钥
        $apiKey = 'sk-416b931b781c420c9f6331b7346962b5';
        echo "\nAPI密钥: " . (substr($apiKey, 0, 5) . '...' . substr($apiKey, -5)) . "\n";
        
    } catch (\Exception $e) {
        echo "数据库连接错误: " . $e->getMessage() . "\n";
    }
} else {
    echo "未找到.env配置文件\n";
}

echo "\n检查完成\n";
?>